/*
 * @Author: along
 */
<template>
    <div class="container shopManage">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-box-content">
                    <p class="header-box-content-title">分类状态:</p>
                    <el-select
                        v-model="head.shop_status"
                        clearable
                        size="small"
                        placeholder="请选择"
                        @change="getTableData(head.shop_status)"
                    >
                        <el-option
                            v-for="item in shopStatus"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
            <div class="header-right">
                <div class="header-box-content">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnCreateManage()"
                    >新建分类组</el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="shopManage"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="title"
                label="分类名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="goods_num"
                label="所属商品数量"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p
                        @click="fnJumpShop(scope.row)"
                        style="color: #409EFF; cursor:pointer"
                    >
                        {{ scope.row.goods_num }}
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序"
                :width="120"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.status == 1">显示</span>
                    <span v-if="scope.row.status == 0">隐藏</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        v-if="scope.row.status == 0"
                        @click="fnSetManage(scope.row, 1)"
                    >
                        显示
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        v-if="scope.row.status == 1"
                        @click="fnSetManage(scope.row, 0)"
                    >
                        隐藏
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditManage(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteManage(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 新建分类组 -->
        <el-dialog
            title="新建分类组"
            :visible.sync="dialogNewBuild"
            width="640px"
            :close-on-click-modal="false"
            class="staff"
        >
            <div class="build">
                <p class="build-title">
                    分类组名称
                </p>
                <el-input
                    v-model="build_name"
                    size="small"
                    :style="{ margin: '12px 24px 0 24px', width: '510px' }"
                />
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="dialogNewBuild = false"
                    size="small"
                    :style="{width: '126px', height: '36px'}"
                >
                    取 消
                </el-button>
                <el-button
                    type="primary"
                    size="small"
                    :style="{width: '126px', height: '36px', marginLeft: '52px'}"
                    :disabled="!build_name"
                    @click="fnAddType()"
                >
                    确 定
                </el-button>
            </span>
        </el-dialog>

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData('')"
            />
        </div> -->
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    data () {
        return {
            build_name: '',
            dialogNewBuild: false,
            cate_no: '',
            id: '',
            type: '',
            head: {
                shop_status: 99
            },
            shopStatus: [{
                label: '全部',
                value: 99
            },{
                label: '已显示',
                value: 1
            },{
                label: '已隐藏',
                value: 0
            }],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            pathName: '',
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {
        dialogVisible (val) {
            if (!val) {
                this.ruleForm = {
                    name: '',
                    manage: []
                };
                this.$refs['ruleForm'].resetFields();
            }
        },
        dialogNewBuild (val) {
            if(!val) {
                this.build_name = '';
            }
        }
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnBreadcrumb();

        if(this.$route.query.cate_no) {
            this.cate_no = this.$route.query.cate_no;
            this.getTableData('');
        }
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '分类管理'}]);
        },

        /**
         * 获取表格数据
         */
        getTableData (type) {
            let list = [];

            this.$post('/student/shop_goods_cate_type%5Cget_list', {
                cate_no: this.cate_no
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;

                    if(type === 1) {
                        this.tableData.map(item=> {
                            if (item.status == 1) {
                                list.push(item);
                            }
                        });
                        this.tableData = list;
                    } else if (type === 0) {
                        this.tableData.map(item=> {
                            if (item.status == 0) {
                                list.push(item);
                            }
                        });
                        this.tableData = list;
                    } else {
                        return;
                    }
                }
            });
        },

        /**
         * 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/student/shop_goods_cate_type%5Cset_sort',{
                    cate_type_no: row.cate_type_no,
                    sort: value
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData('');
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 显示/隐藏分类
         */
        fnSetManage(row, param) {
            this.$post('/student/shop_goods_cate_type%5Cset_status',{
                cate_type_no: row.cate_type_no,
                status: param
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData('');
                    this.$message({
                        type: 'success',
                        message: '设置成功',
                        center: true
                    });
                } else {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 删除分类
         */
        fnDeleteManage(row) {
            this.$confirm('是否删除该分类?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_goods_cate_type%5Cdelete',{
                    cate_type_no: row.cate_type_no
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData('');
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 新建分类
         */
        fnCreateManage () {
            this.type = 'add';
            this.dialogNewBuild = !this.dialogNewBuild;
        },

        /**
         * 编辑分类
         */
        fnEditManage (row) {
            this.id = row.cate_type_no;
            this.type = 'edit';
            this.build_name = row.title;
            this.dialogNewBuild = !this.dialogNewBuild;
        },

        /**
         * 新建/编辑商品组分类
         */
        fnAddType () {
            if(this.type == 'add') {
                this.$post('/student/shop_goods_cate_type%5Cadd', {
                    cate_no: this.cate_no,
                    title: this.build_name
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '添加成功',
                            center: true
                        });
                        this.dialogNewBuild = !this.dialogNewBuild;
                    }else{
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            }

            if(this.type == 'edit') {
                this.$post('/student/shop_goods_cate_type%5Cset', {
                    cate_type_no: this.id,
                    title: this.build_name
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '编辑成功',
                            center: true
                        });
                        this.dialogNewBuild = !this.dialogNewBuild;
                    }else{
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            }
        },

        /**
         * 跳转商品列表
         */
        fnJumpShop (row) {

            let routeData = this.$router.resolve({
                name: 'GoodsList',
                query: {
                    sub_type_no: this.cate_no,
                    cate_type_no: row.cate_type_no,
                    is_group: 2,
                    is_cate: true
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 排序
         */
        handleSortChanged(data) {
            let list = [];

            if (data.order == "descending") {
                list = this.tableData.sort((a,b) => {
                    return a.sort - b.sort;
                });
            } else if (data.order == "ascending") {
                list = this.tableData.sort((a,b) => {
                    return b.sort - a.sort;
                });
            } else {
                this.getTableData('');
            }

            this.tableData = list;
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 15px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-box-content {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .dialog-footer {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .build {
        display: flex;
        flex-direction: column;
        &-title {
            color: #000000;
            font-size: 14px;
            margin-left: 26px;
        }
    }
</style>
<style lang="less">
    .shopManage {
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-checkbox-button__inner {
            border: 1px solid #DCDFE6;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
    }
</style>